package com.cdh.cms.controller.api;

import com.cdh.cms.entity.Ad;
import com.cdh.cms.service.AdService;
import com.cdh.utils.result.Restful;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author by cdh
 * @description:
 * @Date: Created in 11:44 on 2021/3/2
 */
@Api(value = "广告推荐")
@RestController
@RequestMapping("/api/cms/ad")
@CrossOrigin
@Slf4j
public class ApiAdController {

    @Autowired
    private AdService adService;
    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("根据推荐位id显示广告推荐")
    @GetMapping("list/{adTypeId}")
    public Restful listByAdTypeId(
            @ApiParam(value = "推荐位ID",required = true)
            @PathVariable String adTypeId
            ){
        List<Ad> adList = adService.selectByAdTypeId(adTypeId);
        return Restful.ok().data("items",adList);
    }

    @PostMapping("save-redis-test")
    public Restful saveRedisTest(@RequestBody Ad ad){
        redisTemplate.opsForValue().set("ad", ad);
        return Restful.ok();
    }

    @GetMapping("get-redis-test/{key}")
    public Restful getRedisTest(@PathVariable String key){
        Ad ad = (Ad)redisTemplate.opsForValue().get(key);
        return Restful.ok().data("ad",ad);
    }

    @DeleteMapping("delete-redis-test/{key}")
    public Restful deleteRedisTest(@PathVariable String key){
        Boolean delete = redisTemplate.delete(key);
        Boolean hasKey = redisTemplate.hasKey(key);
        return Restful.ok().data("delete",delete).data("hasKey",hasKey);
    }
}
